// pages/homePageInfo/experienceDotsList/editExperienceTypeList/editExperienceTypeList.js
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        id: '',
        showCover: false, //显示弹出框
        coverType: '00', //弹出框类别  '00  新增，01   编辑
        inputContent: '',
        inputValue: '',
        listArr: [],
        refresh: false,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.getExperienceTypeList()
    },


    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },
    onUnload: function() {
        if (this.data.refresh) {
            let pages = getCurrentPages(); //当前页面栈
            let prevPage = pages[pages.length - 2]; //上一页面
            let prevPage2 = pages[pages.length - 3]; //上一页面
            prevPage.refresh()
            prevPage2.refresh()
        }
    },
    getScene(e) {
        this.setData({
            inputValue: e.detail.value
        })
    },
    // 新增分类
    addBtn(e) {
        this.setData({
            showCover: true,
            coverType: '00'
        })
    },
    // 保存
    saveBtn(e) {
        wx.navigateBack({
            delta: 1,
        })
    },
    // 编辑分类
    changeBtn(e) {
        if (e.currentTarget.dataset.id == 'c13239405e064511ab492830cf64d6c7') return
        this.setData({
            showCover: true,
            coverType: '01',
            inputValue: e.currentTarget.dataset.value,
            inputContent: e.currentTarget.dataset.value,
            id: e.currentTarget.dataset.id
        })
    },
    // 删除分类
    deleteBtn(e) {
        let _that = this
        _that.setData({
            id: e.currentTarget.dataset.id
        })
        wx.showModal({
            title: '提示',
            content: '确定删除此类别？删除后此类经验将进入其它类别',
            success: function(res) {
                if (res.confirm) {
                    wx.request({
                        url: app.globalData.rootPath + `cjMap/deleteExperienceType?authId=${app.globalData.authId}`,
                        method: "GET",
                        header: {
                            "content-type": "application/json"
                        },
                        data: {
                            id: _that.data.id,
                            toExperienceTypeId: 'c13239405e064511ab492830cf64d6c7',
                        },
                        success: function(res) {
                            console.log(res)
                            if (res.data.status == 0) {
                                wx.showToast({
                                    title: "删除成功",
                                    icon: "success",
                                    duration: 2000
                                });
                                _that.setData({
                                    refresh: true,
                                })
                            } else {
                                wx.showToast({
                                    title: "删除失败，请重新录入或联系后台管理",
                                    icon: "none",
                                    duration: 2000
                                });
                            }
                            _that.setData({
                                showCover: false,
                                listArr: [],
                                inputValue: ''
                            })
                            _that.getExperienceTypeList()
                        }
                    })
                } else {
                    console.log('点击取消回调')
                }
            }
        })
    },
    //弹窗
    //修改经验
    operateBtn(e) {
        let _that = this
        if (!_that.data.inputValue.length) {
            wx.showToast({
                title: '请填写类名',
                icon: 'none', //如果要纯文本，不要icon，将值设为'none'
                duration: 1500
            })
            return
        }
        if (_that.data.coverType == '01') {
            // 修改
            wx.showModal({
                title: '提示：',
                content: '确认提交修改吗？',
                success: function(res) {
                    if (res.confirm) {
                        wx.request({
                            url: app.globalData.rootPath + `cjMap/editExperienceType?authId=${app.globalData.authId}`,
                            method: "POST",
                            header: {
                                "content-type": "application/json"
                            },
                            data: {
                                typename: _that.data.inputValue,
                                delflg: '00',
                                id: _that.data.id,
                            },
                            success: function(res) {
                                console.log(res)
                                if (res.data.status == 0) {
                                    wx.showToast({
                                        title: "提交成功",
                                        icon: "success",
                                        duration: 2000
                                    });
                                    _that.setData({
                                        refresh: true,
                                    })
                                } else {
                                    wx.showToast({
                                        title: "提交失败，请重新录入或联系后台管理",
                                        icon: "none",
                                        duration: 2000
                                    });
                                }
                                _that.setData({
                                    showCover: false,
                                    listArr: [],
                                    inputValue: ''
                                })
                                _that.getExperienceTypeList()
                            }
                        })
                    } else {
                        console.log('点击取消回调')
                    }
                }
            })
        } else {
            //创建经验
            wx.showModal({
                title: '提示：',
                content: '确认提交吗？',
                success: function(res) {
                    if (res.confirm) {
                        wx.request({
                            url: app.globalData.rootPath + `cjMap/createExperienceType?authId=${app.globalData.authId}`,
                            method: "POST",
                            header: {
                                "content-type": "application/json"
                            },
                            data: {
                                typename: _that.data.inputValue,
                                delflg: '00'
                            },
                            success: function(res) {
                                console.log(res)
                                if (res.data.status == 0) {
                                    wx.showToast({
                                        title: "提交成功",
                                        icon: "success",
                                        duration: 2000
                                    });
                                    _that.setData({
                                        refresh: true,
                                    })
                                } else {
                                    wx.showToast({
                                        title: "提交失败，请重新录入或联系后台管理",
                                        icon: "none",
                                        duration: 2000
                                    });
                                }
                                _that.setData({
                                    showCover: false,
                                    listArr: [],
                                    inputValue: ''
                                })
                                _that.getExperienceTypeList()
                            }
                        })
                    } else {
                        console.log('点击取消回调')
                    }
                }
            })
        }
    },
    hideCover(e) {
        this.setData({
            showCover: false
        })
    },
    stopEvent(e) {
        return
    },
    // 刷新经验
    refresh(){

    },
    //获取经验类别列表
    getExperienceTypeList() {
        let _that = this
        wx.request({
            url: app.globalData.rootPath + `cjMap/getExperienceTypeList?authId=${app.globalData.authId}`,
            method: "GET",
            header: {
                "content-type": "application/json"
            },
            success: function(res) {
                console.log(res)
                _that.setData({
                    listArr: res.data.data,
                })
            }
        })
    }
})